package com.wrupple.muba.catalogs.client.cms;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.user.client.ui.Image;
import com.wrupple.muba.catalogs.client.activity.impl.AbstractCatalogActivity;
import com.wrupple.muba.catalogs.client.activity.process.CatalogEditingProcess;
import com.wrupple.muba.catalogs.client.activity.process.impl.EditionActivityProcessImpl;
import com.wrupple.muba.catalogs.client.activity.process.state.BrowsePlaceInterpret;
import com.wrupple.muba.catalogs.client.activity.process.state.ContentLoadingState;
import com.wrupple.muba.catalogs.client.activity.process.state.InteractionExitState;
import com.wrupple.muba.catalogs.client.activity.process.task.CatalogInteractionState.Mode;
import com.wrupple.muba.catalogs.client.cms.service.CatalogManagementSystem;
import com.wrupple.muba.catalogs.client.module.services.presentation.CatalogPlaceInterpret;
import com.wrupple.muba.catalogs.client.widgets.impl.CatalogTheme;
import com.wrupple.muba.catalogs.domain.CatalogBrowsePlace;
import com.wrupple.muba.catalogs.domain.CatalogUpdatePlace;
import com.wrupple.muba.common.client.activity.ActivityProcess;
import com.wrupple.muba.common.client.application.DataCallback;
import com.wrupple.muba.common.domain.DesktopPlace;
import com.wrupple.muba.common.shared.State.DesktopManager;
import com.wrupple.muba.common.shared.State.ProcessManager;

public class ContentEditingActivity<T extends ProcessManager> extends AbstractCatalogActivity<T> {
	
	public class CancelHandler implements ClickHandler{

		@Override
		public void onClick(ClickEvent event) {
			dm.goTo(new CatalogBrowsePlace(catalog));
		}
		
	}
	
	class UpdateClickHandler implements ClickHandler {

		@Override
		public void onClick(ClickEvent event) {
			
			dm.goTo(new CatalogUpdatePlace(catalog, entryId));
		}
		
	}

	protected BrowsePlaceInterpret read;
	protected InteractionExitState exit;
	private CatalogManagementSystem cms;
	private Mode mode;
	private ContentLoadingState load;


	public ContentEditingActivity(T delegate,CatalogManagementSystem cms,
			CatalogPlaceInterpret placeInterpret,
			DesktopManager dm, BrowsePlaceInterpret read,ContentLoadingState load,InteractionExitState exit, Mode mode) {
		super(delegate, placeInterpret, dm);
		this.read = read;
		this.cms = cms;
		this.exit=exit;
		this.mode=mode;
		this.load=load;
	}



	@Override
	public void getActivityProcess(DesktopPlace item,
			DataCallback<ActivityProcess> callback) {
		CatalogEditingProcess<?> wrappedProcess = getWrappedEditingProcess();
		read.setPlaceInterpret(interpret);
		wrappedProcess.setAction(getActivityActions());
		ActivityProcess result = new EditionActivityProcessImpl(read,load,wrappedProcess ,exit);
		callback.setResultAndFinish(result );
	}
	
	protected CatalogEditingProcess<?> getWrappedEditingProcess() {
		return cms.getContentManager(catalog).getEditingProcess(mode,super.eventBus);
	}



	protected List<? extends HasClickHandlers> getActivityActions() {
		CatalogTheme theme = GWT.create(CatalogTheme.class);

		List<Image> widgets = new ArrayList<Image>(3);
		ClickHandler cancelHandler = new CancelHandler();
		
		
		if(mode==Mode.READ){
			//by convention, the first item gets the sate control R
			widgets.add(new Image(theme.read()));
			widgets.add(super.generateButton(theme.update(), new UpdateClickHandler()));
		}else{
			//by convention, the first item gets the sate control C U
			widgets.add(new Image(theme.ok()));
		}
		
		widgets.add(super.generateButton(theme.editClear(), cancelHandler));

		return widgets;
	}

}
